# -*- coding: utf-8 -*-

import os

lable_file = '../../process/label'
new_lable_file = '../../process/label.txt'

size = 416
grids = 26
grids_size = size / grids
classes = 1
probability = 1
new_lable = open(new_lable_file, 'w')


'''
一个格子五个坐标表示
'''
# def initLabel(label):
#     for i in range(grids * grids * 5):
#         label.append(0)

# for root, dirs, files in os.walk(lable_file):
#     for file in files:
#         labels = open(root + '/' + file)
#         label = []
#         initLabel(label)
#         for line in labels:
#             line = [int(num) for num in line.split()[0:8]]
#             point_x = [num for index, num in enumerate(line) if index % 2 == 0]
#             point_y = [num for index, num in enumerate(line) if index % 2 == 1]
            
#             x = round((sum(point_x) / 4 + 0.5) / grids_size)
#             y = round((sum(point_y) / 4 + 0.5) / grids_size)
#             w = max(point_x) - min(point_x)
#             h = max(point_y) - min(point_y)
#             c = probability
            
#             label[x + y * grids + 0] = x    ###1..26
#             label[x + y * grids + 1] = y    ###1..26
#             label[x + y * grids + 2] = w
#             label[x + y * grids + 3] = h
#             label[x + y * grids + 4] = c

#         file_name = os.path.splitext(file)[0]
#         new_lable.write(file_name + ' ')
#         for x in label:
#             new_lable.write(str(x) + ' ')
#         new_lable.write('\n')
# new_lable.close()

'''
一个格子用一个数表示0-1，表示格子中有目标的概率
'''
def initLabel(label):
    for i in range(grids * grids):
        label.append(0)

for root, dirs, files in os.walk(lable_file):
    for file in files:
        labels = open(root + '/' + file)
        label = []
        initLabel(label)
        for line in labels:
            line = [int(num) for num in line.split()[0:8]]
            point_x = [num for index, num in enumerate(line) if index % 2 == 0]
            point_y = [num for index, num in enumerate(line) if index % 2 == 1]
            
            x = round((sum(point_x) / 4) / grids_size)
            y = round((sum(point_y) / 4) / grids_size)
            
            label[x + y * grids] = 1.0
            if (x - 1 + y * grids) >= 0: 
                label[x - 1 + y * grids] = 1.0
            if (x + 1 + y * grids) < grids * grids:
                label[x + 1 + y * grids] = 1.0
            if (x + (y - 1) * grids) >= 0:
                label[x + (y - 1) * grids] = 1.0
            if (x + (y + 1) * grids) < grids * grids:
                label[x + (y + 1) * grids] = 1.0

        file_name = os.path.splitext(file)[0]
        new_lable.write(file_name + ' ')
        for x in label:
            new_lable.write(str(x) + ' ')
        new_lable.write('\n')
new_lable.close()